package org.jeecg.modules.oauth.xkwoauth.controller;


import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.common.baseservice.SysInfo;
import org.jeecg.modules.courschetemplateatom.service.ICouCourScheTemplateAtomService;
import org.jeecg.modules.oauth.xkwoauth.entity.SysThirdOauthRelationship;
import org.jeecg.modules.oauth.xkwoauth.pojo.OauthResult;
import org.jeecg.modules.oauth.xkwoauth.service.ISysThirdOauthRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: lwz
 * @description: 第三方应用本应用登录
 * @date: 11:48 2018/1/10
 */
@RestController
@RequestMapping("/oauth/xkwoauth/xkwLoginController")
@Api(tags="学科网用户登录")
@Slf4j
public class XkwLoginController {

	@Autowired
	private ISysThirdOauthRelationshipService sysThirdOauthRelationshipService;
	@Autowired
	private SysInfo sysInfo;

	/**
	 * @author: lwz
	 * @description: 跳转登录页面, 当用户已经登陆过第三方应用，则直接跳转用户信息页；反之，跳转登录页面
	 * @date: 11:47 2018/1/10
	 */
	@RequestMapping(value={"","/","/login"},method={RequestMethod.GET,RequestMethod.POST})
	public String toLogin(HttpServletRequest request,HttpSession session,Model model){
		//POST:(@RequestBody Object requestParm,HttpServletRequest req)
		//Map requestParmMap = JSONObject.parseObject(JSONObject.toJSONString(requestParm), Map.class);
		BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(request);
		LoginUser loginUser = baseApiEntity.getLoginUser();

//		User user= new User();//session.getAttribute("userInfo")==null?null:(User)session.getAttribute("userInfo");
//		user.setUserId(loginUser.getId());
//		user.setUserName(loginUser.getUsername());


		if(loginUser == null){
			return "login";
		}
		model.addAttribute("userInfo", loginUser);
		model.addAttribute("hasRelationShip", hasRelationShip(loginUser));
		return "index";
	}
	
	/**
	 * @author: lwz
	 * @description: 根据用户名和密码判断用户是否存在，存在缓存用户信息、执行登陆；反之，前端提示
	 * @date: 11:47 2018/1/10
	 */
	@RequestMapping(value="/isExist",method=RequestMethod.POST)
	@ResponseBody
	public OauthResult isUserExist(HttpServletRequest request,HttpSession session){
		OauthResult resp=new OauthResult();
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(pwd)){
			resp.setMessage("用户名、密码不可为空");
			resp.setData(false);
			return resp;
		}

//		User user=userService.getUserByUserNameAndPwd(userName, pwd);
		BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(request);
		LoginUser loginUser = baseApiEntity.getLoginUser();
		//判定此用户存在，进入index，进行认证；否则，拦截返回登录页面
		if(loginUser != null && loginUser.getId()!=null){
			session.setAttribute("userInfo", loginUser);
			resp.setData(true);
			return resp;
		}
		resp.setMessage("用户名或密码错误");
		resp.setData(false);
		return resp;
	}
	
	/**
	 * @author: lwz
	 * @description: 退出注销
	 * @date: 11:45 2018/1/10
	 */
	@RequestMapping(value="/logout",method={RequestMethod.GET,RequestMethod.POST} )
	public String logout(HttpSession session){
		session.removeAttribute("userInfo");
		return "login";
	}
	
	/**
	 * @author: lwz
	 * @description: 内部方法，判断是否已关联sso相关账号
	 * @return: boolean true:存在关联关系；false:不存在
	 * @date: 11:44 2018/1/10
	 */
	private boolean hasRelationShip(LoginUser loginUser){

//		User user=session.getAttribute("userInfo")==null?null:(User)session.getAttribute("userInfo");
		if(oConvertUtils.isEmpty(loginUser) || oConvertUtils.isEmpty(loginUser.getId())  ){
			return false;
		}
		SysThirdOauthRelationship sysThirdOauthRelationship = sysThirdOauthRelationshipService.getByUserId(loginUser.getId());
		if(oConvertUtils.isEmpty(sysThirdOauthRelationship) ||  oConvertUtils.isEmpty(sysThirdOauthRelationship.getThirdOpenid())){
			return false;
		}
		return true;
	}
	private boolean hasRelationShip(HttpServletRequest request){
		BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(request);
		LoginUser loginUser = baseApiEntity.getLoginUser();
		return hasRelationShip(loginUser);
	}
	/**
	 * @author: lwz
	 * @description: 内部方法，判断是否已关联sso相关账号
	 * @return: boolean true:存在关联关系；false:不存在
	 * @date: 11:44 2018/1/10
	 */
//	private boolean hasRelationShip(User user){
//		if(oConvertUtils.isEmpty(user) || oConvertUtils.isEmpty(user.getUserId())){
//			return false;
//		}
//		RelationShip relationShip = SysThirdOauthRelationship.getByUserId(user.getUserId());
//		if(relationShip ==null){
//			return false;
//		}
//		return true;
//	}
}
